package com.holly.unit.bpmn.designer.core.analysis;

import com.holly.unit.bpmn.designer.constants.BpmnDesignerConstants;
import com.holly.unit.bpmn.designer.core.bpmn.BpmnModelActImpl;
import com.holly.unit.bpmn.designer.core.bpmn.BpmnModelFactory;
import com.holly.unit.bpmn.designer.model.ConditionNode;
import org.activiti.bpmn.model.ExclusiveGateway;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 类描述: 条件解析器 映射为网关
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/5/12 13:37
 */
public class TjNodeAnalysis {

    private BpmnModelFactory factory;

    /***
     * 私有构造方法，禁止用户构造对象
     */
    private TjNodeAnalysis() {
        this.factory = new BpmnModelActImpl();
    }

    /***
     * 内部类持有单例实例，只有在被第一次被使用时才会被初始化
     */
    private static class TjNodeAnalysisHolder{
        /**
         * 静态实例变量保证全局唯一实例,被静态内部类持有
         * (静态内部类在外部类初始化不会被初始化，只有在第一次访问是才会被初始化)
         */
        private static TjNodeAnalysis instance = new TjNodeAnalysis();
    }

    public static TjNodeAnalysis getInstance() {
        return TjNodeAnalysisHolder.instance;
    }

    public List<ExclusiveGateway> createGateWay(List<ConditionNode> conditionNodes) {
        List<ExclusiveGateway> gateways = new ArrayList<>();

        Map<String,List<ConditionNode>> prev = conditionNodes.stream()
                .collect(Collectors.groupingBy(ConditionNode::getPrevId));
        for (String str:prev.keySet()) {
            ExclusiveGateway exclusiveGateway = factory.createExclusiveGateway(str+ BpmnDesignerConstants.GATEWAY_SUFFIX,"");
            gateways.add(exclusiveGateway);
        }
        return gateways;
    }




}
